package com.andnnl.vcode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by chenss on 2020/11/24.
 */
public class ImageRemoveLineTest {

    /*private static Bitmap removeLine(Bitmap img)
    {

        Bitmap bitmap = new Bitmap(img.Width, img.Height);

        for (int x = 0; x < img.Width; x++)
        {
            for (int y = 0; y < img.Height; y++)
            {
                if (x == 0 || y == 0 || x == img.Width - 1 || y == img.Height - 1)
                {
                    bitmap.SetPixel(x, y, Color.White);
                }
                else
                {
                    Color color = img.GetPixel(x, y);
                    if (!isBlack(color.ToArgb()))

                    {
                        bitmap.SetPixel(x, y, Color.White);
                    }
                    else
                        bitmap.SetPixel(x, y, color);
                }

            }
        }

        return bitmap;


    }

    public static bool isBlack(int rgb)
    {
        Color c = Color.FromArgb(rgb);

        int b = c.B;
        int r = c.R;
        int g = c.G;
        int sum = r + g + b;
        if (sum < 5)
        {
            return true;
        }
        return false;
        //sum的值越小（最小为零，黑色）颜色越重，
        //sum的值越大（最大值是225*3）颜色越浅，
        //sum的值小于10就算是黑色了.
    }*/

    public static void main(String[] args) throws IOException {
        String fileName = "picture";
        BufferedImage img = ImageIO.read(new File("D:\\vcode\\3p7e.jpg"));
        int width = img.getWidth();
        int height = img.getHeight();
        for (int i = 1; i < width; i++) {
            Color colorFirst = new Color(img.getRGB(i, 1));
            int numFirstGet = colorFirst.getRed() + colorFirst.getGreen() + colorFirst.getBlue();
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    Color color = new Color(img.getRGB(x, y));
                    System.out.println("red:" + color.getRed() + " | green:" + color.getGreen() + " | blue:" + color.getBlue());
                    int num = color.getRed() + color.getGreen() + color.getBlue();
                    if (num >= numFirstGet) {
                        img.setRGB(x, y, Color.WHITE.getRGB());
                    }
                }
            }
        }


        File file = new File("D:\\vcode\\3p7e-2.jpg");
        if (!file.exists())
        {
            File dir = file.getParentFile();
            if (!dir.exists())
            {
                dir.mkdirs();
            }
            try
            {
                file.createNewFile();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        ImageIO.write(img, "jpg", file);

    }
}
